import { SuperMap3D } from '@/utils/supermap3d';

/**
 * 测量工具类  此方法与 MeasureTool.ts文件功能相同，但使用方法更加简洁，建议使用此方法
 * 使用方法：
 * import { initMeasureHandlers, startDistanceMeasure, startAreaMeasure, startHeightMeasure, stopAll } from '@/utils/measure/measure';
 *
 * onMounted(() => {
 *   // 初始化 viewer 后调用
 *   initMeasureHandlers(viewer, 0)
 * })
 *
 * function measureDistance() {
 *   startDistanceMeasure()
 * }
 *
 * function measureArea() {
 *   startAreaMeasure()
 * }
 *
 * function measureHeight() {
 *   startHeightMeasure()
 * }
 *
 * function clearMeasure() {
 *   stopAll()
 * }
 *
 */

let handlerDis: any | null = null;
let handlerArea: any | null = null;
let handlerHeight: any | null = null;

let viewer: any;
const pickPointEnabled = true;
const clampMode = 0; // 空间模式

function activeMouse(isActive: boolean) {
  if (!viewer) return;
  if (isActive) {
    viewer.enableCursorStyle = false;
    viewer._element.style.cursor = '';
    document.body.classList.add('measureCur');
    viewer.scene.pickPointEnabled = pickPointEnabled;
  } else {
    viewer.enableCursorStyle = true;
    document.body.classList.remove('measureCur');
    viewer.scene.pickPointEnabled = false;
  }
}

export function initMeasureHandlers(viewerInstance: any, clamp: number = clampMode) {
  viewer = viewerInstance;

  // 距离测量
  handlerDis = new SuperMap3D.MeasureHandler(viewer, SuperMap3D.MeasureMode.Distance, clamp);
  handlerDis.measureEvt.addEventListener((result) => {
    const dis = Number(result.distance);
    const distance = dis > 1000 ? (dis / 1000).toFixed(2) + 'km' : dis.toFixed(2) + 'm';
    handlerDis!.disLabel.text = '距离: ' + distance;
  });
  handlerDis.activeEvt.addEventListener(activeMouse);

  // 面积测量
  handlerArea = new SuperMap3D.MeasureHandler(viewer, SuperMap3D.MeasureMode.Area, clamp);
  handlerArea.measureEvt.addEventListener((result) => {
    const areaVal = Number(result.area);
    const area = areaVal > 1000000 ? (areaVal / 1000000).toFixed(2) + 'km²' : areaVal.toFixed(2) + '㎡';
    handlerArea!.areaLabel.text = '面积: ' + area;
  });
  handlerArea.activeEvt.addEventListener(activeMouse);

  // 高度测量
  handlerHeight = new SuperMap3D.MeasureHandler(viewer, SuperMap3D.MeasureMode.DVH);
  handlerHeight.measureEvt.addEventListener((result) => {
    const distance = result.distance > 1000 ? (result.distance / 1000).toFixed(2) + 'km' : result.distance + 'm';
    const vHeight = result.verticalHeight > 1000 ? (result.verticalHeight / 1000).toFixed(2) + 'km' : result.verticalHeight + 'm';
    const hDistance = result.horizontalDistance > 1000 ? (result.horizontalDistance / 1000).toFixed(2) + 'km' : result.horizontalDistance + 'm';

    handlerHeight!.disLabel.text = '空间距离: ' + distance;
    handlerHeight!.vLabel.text = '垂直高度: ' + vHeight;
    handlerHeight!.hLabel.text = '水平距离: ' + hDistance;
  });
  handlerHeight.activeEvt.addEventListener(activeMouse);
}

// 启动测量
export function startDistanceMeasure() {
  stopAll();
  handlerDis?.activate();
}

export function startAreaMeasure() {
  stopAll();
  handlerArea?.activate();
}

export function startHeightMeasure() {
  stopAll();
  handlerHeight?.activate();
}

// 停止所有测量
export function stopAll() {
  handlerDis?.clear();
  handlerDis?.deactivate();
  handlerArea?.clear();
  handlerArea?.deactivate();
  handlerHeight?.clear();
  handlerHeight?.deactivate();
  activeMouse(false);
}
